/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.only.fx;

import com.oim.core.common.component.remote.RemoteClient;
import com.only.common.lib.util.OnlyJsonUtil;

import java.awt.Image;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 *
 * @author Only
 */
public class ScreenCaptureFrame extends javax.swing.JFrame {

 
	private static final long serialVersionUID = 1L;

	/**
     * Creates new form NewJFrame
     */
    public ScreenCaptureFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        iconLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
        bLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.CardLayout());

        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.PAGE_AXIS));

        jPanel1.setLayout(new java.awt.CardLayout());
        jPanel1.add(iconLabel, "card2");

        jScrollPane1.setViewportView(jPanel1);

        jPanel2.add(jScrollPane1);

        jPanel3.setLayout(new java.awt.CardLayout());
        jPanel3.add(bLabel1, "card2");

        jScrollPane2.setViewportView(jPanel3);

        jPanel2.add(jScrollPane2);

        jButton1.setText("Button");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton1);

        getContentPane().add(jPanel2, "card2");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        h();
    }//GEN-LAST:event_jButton1ActionPerformed
    RemoteClient rc = new RemoteClient();

    private void h() {
        ImageBytes ib = new ImageBytes();
        long time=System.currentTimeMillis();
        byte[] bytes = rc.getScreenBytes();
        System.out.println("1:"+(System.currentTimeMillis()-time));
        time=System.currentTimeMillis();
        ImageIcon icon = handle(bytes);
        System.out.println("2:"+(System.currentTimeMillis()-time));
        setTopImage(icon);
        ib.setBytes(bytes);
        time=System.currentTimeMillis();
        String json = OnlyJsonUtil.objectToJson(ib);
        System.out.println("3:"+(System.currentTimeMillis()-time));
        
        time=System.currentTimeMillis();
        ImageBytes b = OnlyJsonUtil.jsonToObject(json, ImageBytes.class);
        System.out.println("4:"+(System.currentTimeMillis()-time));
        
        icon = handle(b.getBytes());
        setBImage(icon);
    }

    public ImageIcon handle(byte[] bytes) {

        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        Image image;
        try {

            image = ImageIO.read(in);
            //image = image.getScaledInstance(480, 320, Image.SCALE_SMOOTH);
            if (null != image) {
                ImageIcon icon = new ImageIcon(image);
                return icon;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private void setTopImage(Icon icon) {
        this.iconLabel.setIcon(icon);
    }

    private void setBImage(Icon icon) {
        this.bLabel1.setIcon(icon);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ScreenCaptureFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ScreenCaptureFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ScreenCaptureFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ScreenCaptureFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(() -> {
            new ScreenCaptureFrame().setVisible(true);
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel bLabel1;
    private javax.swing.JLabel iconLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}
